﻿using System;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Controls;
using System.Collections.Generic;
using WickedFlame.Core.Layers;
using WickedFlame.Core;
using WickedFlame.Core.Shapes;

namespace WickedFlame.Util.Data
{
    public class PngFileFormat : FileFormat
    {
        public override void Export(DrawingCanvas canvas, Stream stream)
        {
            PngBitmapEncoder encoder = new PngBitmapEncoder();

            // Get the size of canvas
            Size size = new Size(canvas.Width, canvas.Height);

            // Create a render bitmap and push the surface to it
            RenderTargetBitmap renderBitmap =
              new RenderTargetBitmap(
                (int)size.Width,
                (int)size.Height,
                96d,
                96d,
                PixelFormats.Pbgra32);

            //foreach (Canvas surface in surfaces)
            //foreach (FrameworkElement surface in surfaces)
            foreach (Layer layer in canvas.Layers)
            {
                // Measure and arrange the surface
                layer.Measure(size);
                layer.Arrange(new Rect(size));

                //render/push each canvas
                if (layer.IsVisible)
                    renderBitmap.Render(layer);
            }

            encoder.Interlace = PngInterlaceOption.On;
            encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
            // save the data to the stream
            encoder.Save(stream);
        }

        //public void ExportToPng(Canvas surface, Stream stream)
        //{
        //    // Save current canvas transform
        //    Transform transform = surface.LayoutTransform;
        //    // reset current transform (in case it is scaled or rotated)
        //    surface.LayoutTransform = null;

        //    // Get the size of canvas
        //    Size size = new Size(surface.Width, surface.Height);
        //    // Measure and arrange the surface
        //    // VERY IMPORTANT
        //    surface.Measure(size);
        //    surface.Arrange(new Rect(size));

        //    // Create a render bitmap and push the surface to it
        //    RenderTargetBitmap renderBitmap =
        //      new RenderTargetBitmap(
        //        (int)size.Width,
        //        (int)size.Height,
        //        96d,
        //        96d,
        //        PixelFormats.Pbgra32);
        //    renderBitmap.Render(surface);

        //    // Create a file stream for saving image
        //    //using (FileStream outStream = new FileStream(path.LocalPath, FileMode.OpenOrCreate))
        //    //{
        //    // Use png encoder for our data
        //    PngBitmapEncoder encoder = new PngBitmapEncoder();
        //    encoder.Interlace = PngInterlaceOption.On;
        //    // push the rendered bitmap to it
        //    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        //    // save the data to the stream
        //    //encoder.Save(outStream);
        //    encoder.Save(stream);
        //    //}

        //    // Restore previously saved layout
        //    surface.LayoutTransform = transform;
        //}

        public override DrawingCanvas Import(Stream stream)
        {
            PngBitmapDecoder pngdecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

            stream.Seek(0, System.IO.SeekOrigin.Begin);

            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.StreamSource = stream;
            image.EndInit();

            var canvas = new DrawingCanvas();
            canvas.Width = image.Width;
            canvas.Height = image.Height;
            canvas.AddLayer();

            var shape = new ImageShape(image);
            canvas.AddShape(shape, true);
            shape.Invalidate();

            return canvas;
        }
    }
}
